﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNetNuke.UI.WebControls;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Modules;
using DNNToolkit.Modules.CityTaxLocator;
using DNNToolkit.Modules.CityTaxLocator.Components;

namespace DNNToolkit.Modules.CityTaxLocator.Controls
{
    public partial class AdditionResourceControl : System.Web.UI.UserControl
    {
        #region "Private Methods"

        //Local tag cache to avoid multiple generations of the same tag

        #endregion

        #region "Event Handlers"

        private PortalModuleBase Module
        {
            get
            {
                if (Session["Module"] != null)
                    return (PortalModuleBase)Session["Module"];
                return null;
            }
            set
            {
                Session["Module"] = value;
            }
        }

        private CityTaxCommentInfo CityTaxComment
        {
            get
            {
                if (Session["CityTaxComment"] == null)
                    return null;
                return (CityTaxCommentInfo)Session["CityTaxComment"];
            }
            set { Session["CityTaxComment"] = value; }
        }

        public void ShowMessage(PortalModuleBase module)
        {
            Visible = true;
            Module = module;
            // get HtmlText object
            CityTaxLocatorController controller = new CityTaxLocatorController();
            lblContent.EditEnabled = Module.IsEditable && controller.GetCityTaxRecordCount() > 0;
            CityTaxComment = controller.GetCityTaxComment();
            // get default content from resource 
            if (lblContent.EditEnabled)
            {
                //localize toolbar
                foreach (DotNetNuke.UI.WebControls.DNNToolBarButton objButton in this.tbEIPHTML.Buttons)
                {
                    objButton.ToolTip = DotNetNuke.Services.Localization.Localization.GetString("cmd" + objButton.ToolTip);
                }
            }
            string strContent = "";
            if (CityTaxComment != null)
            {
                strContent = string.IsNullOrEmpty(CityTaxComment.AdditionComment) ? "[Addition Comment]" : CityTaxComment.AdditionComment;
            }
            lblContent.Controls.Add(new LiteralControl(strContent));


            // menu action handler
            DotNetNuke.UI.Skins.Skin ParentSkin = DotNetNuke.UI.Skins.Skin.GetParentSkin(Module);
            //We should always have a ParentSkin, but need to make sure
            if (ParentSkin != null)
            {
                //Register our EventHandler as a listener on the ParentSkin so that it may tell us when a menu has been clicked.
                ParentSkin.RegisterModuleActionEvent(Module.ModuleId, ModuleAction_Click);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// lblContent_UpdateLabel allows for inline editing of content.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void lblContent_UpdateLabel(object source, DotNetNuke.UI.WebControls.DNNLabelEditEventArgs e)
        {
            CityTaxLocatorController controller = new CityTaxLocatorController();
            CityTaxCommentInfo comment = new CityTaxCommentInfo();
            if (CityTaxComment != null)
                comment = CityTaxComment;
            
            // set content values
            if (e.Text != "[Addition Comment]")
            {
                comment.AdditionComment = e.Text;
                controller.UpdateCityTaxCommentInfo(comment);
            }
            ModuleController.SynchronizeModule(Module.ModuleId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ModuleAction_Click handles all ModuleAction events raised from the skin.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void ModuleAction_Click(object sender, ActionEventArgs e)
        {
            if (e.Action.Url.Length > 0)
            {
                Response.Redirect(e.Action.Url, true);
            }
        }

        #endregion
    }
}